<?php

namespace app\api\controller;

/**
 * ============================================================================
 * DSKMS多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 发票控制器
 */
class  Memberinvoice extends MobileMember
{
    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * @api {POST} api/Memberinvoice/invoice_list 发票信息列表
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     * 
     * @apiParam {Int} page 页码
     * @apiParam {Int} per_page 每页数量
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {Object[]} result.invoice_list  发票列表
     * @apiSuccess {String} result.invoice_list.invoice_code  纳税人识别号[普通发票]
     * @apiSuccess {String} result.invoice_list.invoice_company  单位名称
     * @apiSuccess {String} result.invoice_list.invoice_company_code  纳税人识别号
     * @apiSuccess {String} result.invoice_list.invoice_content  发票内容[普通发票]
     * @apiSuccess {String} result.invoice_list.invoice_goto_addr  送票地址
     * @apiSuccess {Int} result.invoice_list.invoice_id  发票信息ID
     * @apiSuccess {String} result.invoice_list.invoice_rec_mobphone  收票人手机号
     * @apiSuccess {String} result.invoice_list.invoice_rec_name  收票人姓名
     * @apiSuccess {String} result.invoice_list.invoice_rec_province  收票人省份
     * @apiSuccess {String} result.invoice_list.invoice_reg_addr  注册地址
     * @apiSuccess {String} result.invoice_list.invoice_reg_baccount  银行帐户
     * @apiSuccess {String} result.invoice_list.invoice_reg_bname  开户银行
     * @apiSuccess {String} result.invoice_list.invoice_reg_phone  注册电话
     * @apiSuccess {Int} result.invoice_list.invoice_state  发票类型 1:普通发票 2:增值税发票
     * @apiSuccess {String} result.invoice_list.invoice_title  发票抬头[普通发票]
     * @apiSuccess {Int} result.invoice_list.member_id 用户ID
     */
    public function invoice_list()
    {
        $invoice_model = model('invoice');
        $condition = array();
        $condition[] = array('member_id','=',$this->member_info['member_id']);
        $invoice_list = $invoice_model->getInvoiceList($condition, 10, '*');
        ds_json_encode(10000, '',array('invoice_list' => $invoice_list));
    }

    /**
     * @api {POST} api/Memberinvoice/invoice_info 发票信息详情
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} invoice_id 发票ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     * @apiSuccess {String} result.invoice_code  纳税人识别号[普通发票]
     * @apiSuccess {String} result.invoice_company  单位名称
     * @apiSuccess {String} result.invoice_company_code  纳税人识别号
     * @apiSuccess {String} result.invoice_content  发票内容[普通发票]
     * @apiSuccess {String} result.invoice_goto_addr  送票地址
     * @apiSuccess {Int} result.invoice_id  发票信息ID
     * @apiSuccess {String} result.invoice_rec_mobphone  收票人手机号
     * @apiSuccess {String} result.invoice_rec_name  收票人姓名
     * @apiSuccess {String} result.invoice_rec_province  收票人省份
     * @apiSuccess {String} result.invoice_reg_addr  注册地址
     * @apiSuccess {String} result.invoice_reg_baccount  银行帐户
     * @apiSuccess {String} result.invoice_reg_bname  开户银行
     * @apiSuccess {String} result.invoice_reg_phone  注册电话
     * @apiSuccess {Int} result.invoice_state  发票类型 1:普通发票 2:增值税发票
     * @apiSuccess {String} result.invoice_title  发票抬头[普通发票]
     * @apiSuccess {Int} result.member_id 用户ID
     */
    public function invoice_info() {
        $invoice_id = intval(input('post.invoice_id'));
        if ($invoice_id <= 0) {
            ds_json_encode(10001, '参数错误');
        }
        $invoice_model = model('invoice');
        $result = $invoice_model->getInvoiceInfo(array('invoice_id' => $invoice_id, 'member_id' => $this->member_info['member_id']));
        ds_json_encode(10000, '', $result);
    }
    /**
     * @api {POST} api/Memberinvoice/invoice_del 发票信息删除
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} invoice_id 发票ID
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function invoice_del()
    {
        $invoice_id = intval(input('post.invoice_id'));
        if ($invoice_id <= 0) {
            ds_json_encode(10001,'参数错误');
        }

        $invoice_model = model('invoice');

        $result = $invoice_model->delInvoice(array('invoice_id' => $invoice_id, 'member_id' => $this->member_info['member_id']));
        if ($result) {
            ds_json_encode(10000, '',1);
        }
        else {
            ds_json_encode(10001,'删除失败');
        }
    }

    /**
     * @api {POST} api/Memberinvoice/invoice_add 发票信息添加
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {String} invoice_state 发票类型
     * @apiParam {String} invoice_title 发票抬头[普通发票]
     * @apiParam {String} invoice_code 纳税人识别号[普通发票]
     * @apiParam {String} invoice_content 发票内容[普通发票]
     * @apiParam {String} invoice_company 单位名称
     * @apiParam {String} invoice_company_code 纳税人识别号
     * @apiParam {String} invoice_reg_addr 注册地址
     * @apiParam {String} invoice_reg_phone 注册电话
     * @apiParam {String} invoice_reg_bname 开户银行
     * @apiParam {String} invoice_reg_baccount 银行帐户
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function invoice_add()
    {
        $invoice_model = model('invoice');

        $data = array();
        $data['invoice_state'] = input('post.invoice_state');
        $data['invoice_title'] = input('post.invoice_title');
        $data['invoice_content'] = input('post.invoice_content');
        $data['invoice_code'] = input('post.invoice_code');
        $data['invoice_company'] = input('post.invoice_company');
        $data['invoice_company_code'] = input('post.invoice_company_code');
        $data['invoice_reg_addr'] = input('post.invoice_reg_addr');
        $data['invoice_reg_phone'] = input('post.invoice_reg_phone');
        $data['invoice_reg_bname'] = input('post.invoice_reg_bname');
        $data['invoice_reg_baccount'] = input('post.invoice_reg_baccount');
        $data['member_id'] = $this->member_info['member_id'];
        
        //验证器
        $scene = '';
        if ($data['invoice_state'] == 1) {
            $scene = 'invoice_1_update';
        } elseif ($data['invoice_state'] == 2) {
            $scene = 'invoice_2_update';
        } else {
            ds_json_encode(10001, lang('param_error'));
        }
        $memberinvoice_validate = ds_validate('invoice');
        if (!$memberinvoice_validate->scene($scene)->check($data)) {
            ds_json_encode(10001, $memberinvoice_validate->getError());
        }
        
        $result = $invoice_model->addInvoice($data);
        if ($result) {
            ds_json_encode(10000, '',array('invoice_id' => $result));
        }
        else {
            ds_json_encode(10001,'添加失败');
        }
    }

    /**
     * @api {POST} api/Memberinvoice/invoice_edit 发票信息编辑
     * @apiVersion 1.0.0
     * @apiGroup MemberInvoice
     *
     * @apiHeader {String} X-DS-KEY 用户授权token
     *
     * @apiParam {Int} invoice_id 发票ID
     * @apiParam {String} invoice_state 发票类型
     * @apiParam {String} invoice_title 发票抬头[普通发票]
     * @apiParam {String} invoice_code 纳税人识别号[普通发票]
     * @apiParam {String} invoice_content 发票内容[普通发票]
     * @apiParam {String} invoice_company 单位名称
     * @apiParam {String} invoice_company_code 纳税人识别号
     * @apiParam {String} invoice_reg_addr 注册地址
     * @apiParam {String} invoice_reg_phone 注册电话
     * @apiParam {String} invoice_reg_bname 开户银行
     * @apiParam {String} invoice_reg_baccount 银行帐户
     *
     * @apiSuccess {String} code 返回码,10000为成功
     * @apiSuccess {String} message  返回消息
     * @apiSuccess {Object} result  返回数据
     */
    public function invoice_edit() {
        $invoice_id = intval(input('post.invoice_id'));
        $invoice_model = model('invoice');
        $data['invoice_state'] = input('post.invoice_state');
        $data['invoice_title'] = input('post.invoice_title');
        $data['invoice_content'] = input('post.invoice_content');
        $data['invoice_code'] = input('post.invoice_code');
        $data['invoice_company'] = input('post.invoice_company');
        $data['invoice_company_code'] = input('post.invoice_company_code');
        $data['invoice_reg_addr'] = input('post.invoice_reg_addr');
        $data['invoice_reg_phone'] = input('post.invoice_reg_phone');
        $data['invoice_reg_bname'] = input('post.invoice_reg_bname');
        $data['invoice_reg_baccount'] = input('post.invoice_reg_baccount');
        
        //验证器
        $scene = '';
        if ($data['invoice_state'] == 1) {
            $scene = 'invoice_1_update';
        } elseif ($data['invoice_state'] == 2) {
            $scene = 'invoice_2_update';
        } else {
            ds_json_encode(10001, lang('param_error'));
        }
        $memberinvoice_validate = ds_validate('invoice');
        if (!$memberinvoice_validate->scene($scene)->check($data)) {
            ds_json_encode(10001, $memberinvoice_validate->getError());
        }
        
        $result = $invoice_model->editInvoice($data, array('invoice_id' => $invoice_id,'member_id'=>$this->member_info['member_id']));
        if ($result) {
            ds_json_encode(10000,'');
        }
        else {
            ds_json_encode(10001,'保存失败');
        }
    }
    /**
     * 发票内容列表
     */
    public function invoice_content_list()
    {
        $invoice_content_list = array(
            '明细', '酒', '食品', '饮料', '玩具', '日用品', '装修材料', '化妆品', '办公用品', '学生用品', '家居用品', '饰品', '服装', '箱包', '精品', '家电',
            '劳防用品', '耗材', '电脑配件'
        );
        ds_json_encode(10000, '',array('invoice_content_list' => $invoice_content_list));
    }
}